package com.ys.web.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.Conjunction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ys.WebConstants;
import com.ys.extend.utils.YSBeanUtil;
import com.ys.web.dao.CommonDao;
import com.ys.web.entity.OriginBaidu;
import com.ys.web.pojo.BaiduPromoteInfo;
import com.ys.web.pojo.CompanyInfo;
import com.ys.web.remotemodel.RemoteAccountInfo;
import com.ys.web.service.OriginBaiduService;
import com.ys.web.ws.service.ManageCompanyInfoService;
import com.ys.web.ws.service.ManageIndustryService;

/**
 * 百度消费数据表(原始数据) -Service-实现类
 * @author wdj
 *
 */
@Service("originBaiduService")
@Transactional
public class OriginBaiduServiceImpl implements OriginBaiduService {
	
	@Autowired
	private CommonDao commonDao;
	@Resource
	private ManageIndustryService manageIndustryService;
	@Resource
	private ManageCompanyInfoService manageCompanyInfoService;
	
 	/**
 	 * 批量新增
 	 * @param entitys
 	 */
	@Override
	public void batchSave(List<OriginBaidu> entitys) {
		commonDao.batchSave(entitys);
	}

	@Override
	public void update(OriginBaidu originBaidu) {
		commonDao.updateEntitie(originBaidu);
	}
	
	@Override
	public OriginBaidu ifExistOriginBaidu(Integer companyId, Date promotionDate,
						String promotionPlan, String promotionKeyword, String keywordSearch) {
		DetachedCriteria dc = DetachedCriteria.forClass(OriginBaidu.class);
		if(companyId!=null && promotionDate!=null) {			
			Conjunction conjunction = Restrictions.conjunction();
			conjunction.add(Restrictions.eq("companyId", companyId));
			conjunction.add(Restrictions.eq("promotionDate", promotionDate));
			conjunction.add(Restrictions.eq("promotionPlan", promotionPlan));
			conjunction.add(Restrictions.eq("promotionKeyword", promotionKeyword));
			conjunction.add(Restrictions.eq("keywordSearch", keywordSearch));
			dc.add(conjunction);
		}
		else {
			try {
				throw new Exception("参数为空 ！");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		//只返回一条记录
		List<OriginBaidu> list = commonDao.pageList(dc, 0, 1);
		if(list != null && !list.isEmpty()) {
			return list.get(0);
		}
		else {
			return null;				
		}
	}

 	/**
 	 * 获取百度消费数据并计算所属象限,默认返回1000条
 	 * @param accountId 代运营公司账号id
 	 * @param dateBegin 开始日期
 	 * @param dateEnd 结束日期
 	 * @return 1000条百度消费数据记录

	@Override
 	public List<BaiduPromoteInfo> getLimitBaiduPromoteInfo(Integer accountId,Date dateBegin, Date dateEnd) {
		List<BaiduPromoteInfo> listInfo = new ArrayList<>();
		String hql = " from OriginBaidu where accountId = ? and dateBegin >= ? and dateEnd <= ?";
//		List<OriginBaidu> list = commonDao.findHqlByLimit(hql, 0, WebConstants.MAX_REQUEST_ROW_EACH_PROMOTE, accountId, dateBegin, dateEnd);

		DetachedCriteria dc = DetachedCriteria.forClass(OriginBaidu.class);
		if(accountId!=null && dateBegin!=null && dateEnd!=null){
			dc.add(Restrictions.and(Restrictions.eq("accountId", accountId)),
					);

		}

		List<OriginBaidu> list = commonDao.pageList(dc, 0, WebConstants.MAX_REQUEST_ROW_EACH_PROMOTE);
		
		Account account =commonDao.getEntity(Account.class, accountId);
		Company company = commonDao.getEntity(Company.class, account.getCompanyId());
		for (OriginBaidu originBaidu : list) {
			BaiduPromoteInfo baiduPromoteInfo = new BaiduPromoteInfo();
			try {
				YSBeanUtil.copyBeanNotNull2BeanExactly(originBaidu, baiduPromoteInfo);
			} catch (Exception e) {
				e.printStackTrace();
			}
			baiduPromoteInfo.setCompanyName(company.getCompanyName());
			//计算所属象限
			calculateQuadrant_DB(company.getIndustryId(),baiduPromoteInfo);
			listInfo.add(baiduPromoteInfo);
		}
		return listInfo;
	}  	 */

	/**
	 * 根据数据库的四象限边界值 计算所属象限，存入pojo类
	 * @param industryId 行业id
	 * @param baiduPromoteInfo  pojo类

	private void calculateQuadrant_DB(Integer industryId, BaiduPromoteInfo baiduPromoteInfo) {		
		List<Quadrant> list = commonDao.findByProperty(Quadrant.class, "industryId", industryId);
		double consumeRateMin_1 = 0; //最小消费转化比（总转化／消费金额）
		double consumeRateMax_1 = 0; //最大消费转化比（总转化／消费金额）
		double consumeRateMin_2 = 0;
		double consumeRateMax_2 = 0;
		double consumeRateMin_3 = 0;
		double consumeRateMax_3 = 0;
		double consumeRateMin_4 = 0;
		double consumeRateMax_4 = 0;
		String quadrantTag_1 = ""; //象限标志(唯一)
		String quadrantTag_2 = "";
		String quadrantTag_3 = "";
		String quadrantTag_4 = "";
		for (Quadrant quadrant : list) {			
			switch (quadrant.getTag()) {
			case "1":
				consumeRateMin_1 =quadrant.getConsumeRateMin();
				consumeRateMax_1 =quadrant.getConsumeRateMax();
				quadrantTag_1 = quadrant.getTag();
				break;
			case "2":
				consumeRateMin_2 =quadrant.getConsumeRateMin();
				consumeRateMax_2 =quadrant.getConsumeRateMax();				
				quadrantTag_2 = quadrant.getTag();
				break;
			case "3":
				consumeRateMin_3 =quadrant.getConsumeRateMin();
				consumeRateMax_3 =quadrant.getConsumeRateMax();				
				quadrantTag_3 = quadrant.getTag();
				break;
			case "4":
				consumeRateMin_4 =quadrant.getConsumeRateMin();
				consumeRateMax_4 =quadrant.getConsumeRateMax();				
				quadrantTag_4 = quadrant.getTag();
				break;
			default:
				break;
			}
		}
		
		//消费转化比
		double consumeConsultRate = baiduPromoteInfo.getConsumeConsultRate();
		//计算所属象限
		if(consumeConsultRate>consumeRateMin_1 && consumeConsultRate<consumeRateMax_1) {
			baiduPromoteInfo.setQuadrantTag(quadrantTag_1);
		}
		else if (consumeConsultRate>consumeRateMin_2 && consumeConsultRate<consumeRateMax_2) {
			baiduPromoteInfo.setQuadrantTag(quadrantTag_2);		
		}
		else if (consumeConsultRate>consumeRateMin_3 && consumeConsultRate<consumeRateMax_3) {
			baiduPromoteInfo.setQuadrantTag(quadrantTag_3);		
		}
		else if (consumeConsultRate>consumeRateMin_4 && consumeConsultRate<consumeRateMax_4) {
			baiduPromoteInfo.setQuadrantTag(quadrantTag_4);		
		}
		else if(consumeConsultRate == 0) {
			baiduPromoteInfo.setQuadrantTag("直接删除");					
		}
		else {
			baiduPromoteInfo.setQuadrantTag("其他");							
		}		
	} 	 */

	
 	/**
 	 * 获取百度消费数据（根据账号和推广时段），并根据页面边界值计算所属象限，默认返回1000条
 	 * @param accountId 代运营公司账号id
 	 * @param dateBegin 开始日期
 	 * @param dateEnd 结束日期
 	 * @param currentPage 当前页码
 	 * @param quadrantParams 四象限边界值
 	 * @return 1000条百度消费数据记录

 	public List<BaiduPromoteInfo> getLimitBaiduPromoteInfo(Integer accountId,Date dateBegin, Date dateEnd,int currentPage,
				double consumeRateMin_1,double consumeRateMin_2,double consumeRateMin_3,double consumeRateMin_4,
				double consumeRateMax_1,double consumeRateMax_2,double consumeRateMax_3,double consumeRateMax_4) {
		List<BaiduPromoteInfo> listInfo = new ArrayList<>();
		
		List<OriginBaidu> list = new ArrayList<>();
		DetachedCriteria dc = DetachedCriteria.forClass(OriginBaidu.class);
		if(accountId!=null ) {
			Criterion cr = Restrictions.eq("accountId", accountId);			
			dc.add(cr);
		}
		if(dateBegin!=null && dateEnd!=null) {
			Conjunction conjunction = Restrictions.conjunction();
			conjunction.add(Restrictions.ge("dateBegin", dateBegin));
			conjunction.add(Restrictions.le("dateEnd", dateEnd));
			dc.add(conjunction);
		}
		
		dc.addOrder(Order.desc("dateBegin")); //按“开始时间”降序
		list = commonDao.pageList(dc, WebConstants.MAX_REQUEST_ROW_EACH_PROMOTE*(currentPage-1), WebConstants.MAX_REQUEST_ROW_EACH_PROMOTE);			

		for (OriginBaidu originBaidu : list) {
			BaiduPromoteInfo baiduPromoteInfo = new BaiduPromoteInfo();
			try {
				YSBeanUtil.copyBeanNotNull2BeanExactly(originBaidu, baiduPromoteInfo);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			Account account =commonDao.getEntity(Account.class, originBaidu.getAccountId());			
//			System.out.println("accountId--"+originBaidu.getAccountId());
//			System.out.println("commpanyId--"+account.getCompanyId());
			
			Company company = commonDao.getEntity(Company.class, account.getCompanyId());
			baiduPromoteInfo.setCompanyName(company.getCompanyName());
			baiduPromoteInfo.setLoginName(account.getLoginName());
			//根据页面边界值计算所属象限
			calculateQuadrant_Front(baiduPromoteInfo, 
					consumeRateMin_1, consumeRateMin_2, consumeRateMin_3, consumeRateMin_4, 
					consumeRateMax_1, consumeRateMax_2, consumeRateMax_3, consumeRateMax_4);
			listInfo.add(baiduPromoteInfo);
		}
		return listInfo; 		
 		
 	} 	 */
 	

 	/**
 	 * 根据页面边界值计算所属象限
 	 * @param baiduPromoteInfo 百度消费数据实体
 	 * @param quadrantParams
 	 */
	private void calculateQuadrant_Front(BaiduPromoteInfo baiduPromoteInfo, 				
					double consumeRateMin_1,double consumeRateMin_2,double consumeRateMin_3,double consumeRateMin_4,
					double consumeRateMax_1,double consumeRateMax_2,double consumeRateMax_3,double consumeRateMax_4) {
		//消费转化比
		double consumeConsultRate = baiduPromoteInfo.getConsumeConsultRate();
		
		//计算所属象限
		if(consumeConsultRate>consumeRateMin_1 && consumeConsultRate<consumeRateMax_1) {
			baiduPromoteInfo.setQuadrantTag("1");
		}
		else if (consumeConsultRate>consumeRateMin_2 && consumeConsultRate<consumeRateMax_2) {
			baiduPromoteInfo.setQuadrantTag("2");		
		}
		else if (consumeConsultRate>consumeRateMin_3 && consumeConsultRate<consumeRateMax_3) {
			baiduPromoteInfo.setQuadrantTag("3");		
		}
		else if (consumeConsultRate>consumeRateMin_4 && consumeConsultRate<consumeRateMax_4) {
			baiduPromoteInfo.setQuadrantTag("4");		
		}
		else if(consumeConsultRate == 0) {
			baiduPromoteInfo.setQuadrantTag("直接删除");					
		}
		else {
			baiduPromoteInfo.setQuadrantTag("其他");							
		}			
	}
	
 	
 	/**
 	 * 获取百度消费数据（根据公司和推广时段），并根据页面边界值计算所属象限，默认返回1000条
 	 * @param companyId 推广公司id
 	 * @param dateBegin 开始日期
 	 * @param dateEnd 结束日期
 	 * @param currentPage 当前页码
 	 * @param consumeRateMin_1 最小消费转化比
 	 * @param consumeRateMin_2
 	 * @param consumeRateMin_3
 	 * @param consumeRateMin_4
 	 * @param consumeRateMax_1 最大消费转化比
 	 * @param consumeRateMax_2
 	 * @param consumeRateMax_3
 	 * @param consumeRateMax_4
 	 * @return 返回百度消费数据记录
 	 */
	@Override
 	public List<BaiduPromoteInfo> getLimitBaiduInfoByCompanyId(Integer companyId, Date dateBegin, Date dateEnd, int currentPage,
				double consumeRateMin_1,double consumeRateMin_2,double consumeRateMin_3,double consumeRateMin_4,
				double consumeRateMax_1,double consumeRateMax_2,double consumeRateMax_3,double consumeRateMax_4) {
 		List<BaiduPromoteInfo> listInfo = new ArrayList<>();
 		
		DetachedCriteria dc = DetachedCriteria.forClass(OriginBaidu.class);
		if(companyId!=null) {
			Criterion cr = Restrictions.eq("companyId", companyId);			
			dc.add(cr);
		}
		if(dateBegin!=null && dateEnd!=null) {
			Conjunction conjunction = Restrictions.conjunction();
			conjunction.add(Restrictions.ge("promotionDate", dateBegin));
			conjunction.add(Restrictions.le("promotionDate", dateEnd));
			dc.add(conjunction);
		}
		dc.addOrder(Order.desc("promotionDate")); //按“推广时间”降序
		
		List<OriginBaidu> list = commonDao.pageList(dc, WebConstants.MAX_REQUEST_ROW_EACH_PROMOTE*(currentPage-1), WebConstants.MAX_REQUEST_ROW_EACH_PROMOTE);				
		String companyName = "";
		String industryId ="";
		String industryName = "";
		String loginName = "";
		for (OriginBaidu originBaidu : list) {
			try {
				BaiduPromoteInfo baiduPromoteInfo = new BaiduPromoteInfo();
				YSBeanUtil.copyBeanNotNull2BeanExactly(originBaidu, baiduPromoteInfo);
				if(companyName!=null && companyName.length()==0) {
					CompanyInfo company = manageCompanyInfoService.getEntity(originBaidu.getCompanyId());
					companyName = company.getCompanyName();
					industryId = company.getIndustryId();
					industryName = company.getIndustry();
				}
				if(loginName!=null && loginName.length()==0) {
					List<RemoteAccountInfo> accountList = manageCompanyInfoService.getAccountByCompanyId(originBaidu.getCompanyId());	
					RemoteAccountInfo account = accountList.get(0);					
					loginName = account.getLoginName();
				}
				
				baiduPromoteInfo.setCompanyName(companyName);
				baiduPromoteInfo.setIndustryId(industryId);
				baiduPromoteInfo.setIndustryName(industryName);
				baiduPromoteInfo.setLoginName(loginName);
				//根据页面边界值计算所属象限
				calculateQuadrant_Front(baiduPromoteInfo, 
						consumeRateMin_1, consumeRateMin_2, consumeRateMin_3, consumeRateMin_4, 
						consumeRateMax_1, consumeRateMax_2, consumeRateMax_3, consumeRateMax_4);
				listInfo.add(baiduPromoteInfo);
			} 
			catch (Exception e) {
				e.printStackTrace();
			}		
		}				
 		
 		return listInfo;
 	}

 	
}
